package com.example.gzysmartcity.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.gzysmartcity.R;
import com.example.gzysmartcity.databinding.ActivityLoginBinding;
import com.example.gzysmartcity.utils.NetworkUtil;
import com.example.gzysmartcity.viewmodel.LoginViewModel;

public class LoginActivity extends AppCompatActivity {
    private SharedPreferences.Editor editor;
    private ActivityLoginBinding b;
    private SharedPreferences sp;

    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        switch (SPUtils.getInstance("theme", Context.MODE_PRIVATE).getInt("theme", 1)) {
            case 1:
                setTheme(R.style.Theme_GZYSmartCity);
                break;
            case 2:
                setTheme(R.style.Theme_GZYSmartCity_Yellow);
                break;
            case 3:
                setTheme(R.style.Theme_GZYSmartCity_Green);
                break;
            case 4:
                setTheme(R.style.Theme_GZYSmartCity_Blue);
                break;
        }
        super.onCreate(savedInstanceState);
        b = ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(b.getRoot());
        Log.i("TAG", "LoginActivity: ");
        //保存密码
        sp = getSharedPreferences("User", Context.MODE_PRIVATE);
        boolean checkLogin = sp.getBoolean("checkLogin", false);
        if (checkLogin) {
            NetworkUtil.userId = sp.getString("userid", null);
            startActivity(new Intent(LoginActivity.this, MainActivity.class));
            finish();
        }
        b.igLoginExit.setOnClickListener(v -> {
            startActivity(new Intent(this, MainActivity.class));
            finish();
        });
        boolean checkPass = sp.getBoolean("checkPass", false);
        if (checkPass) {
            b.checkPass.setChecked(true);
            b.editUsername.setText(sp.getString("user", ""));
            b.editPassword.setText(sp.getString("pass", ""));
            b.editUserId.setText(sp.getString("userid", ""));
        }
        //自动登录
        LoginViewModel loginViewModel = new ViewModelProvider(this).get(LoginViewModel.class);
        b.btnLogin.setOnClickListener(v -> {
            String user = b.editUsername.getText().toString().trim();
            String pass = b.editPassword.getText().toString().trim();
            String userid = b.editUserId.getText().toString().trim();
            if (user.equals("")) {
                ToastUtils.showShort("请输入用户名");
                return;
            }
            if (pass.equals("")) {
                ToastUtils.showShort("请输入密码");
                return;
            }
            if (userid.equals("")) {
                ToastUtils.showShort("请输入用户ID");
                return;
            }
            rememberPassword(b.checkPass.isChecked());
            loginViewModel.getLogin(user, pass);
        });
        loginViewModel.getLiveData().observe(this, state -> {
            switch (state) {
                case SUCCESS:
                    ToastUtils.showShort("登录成功");
                    NetworkUtil.userId = b.editUserId.getText().toString().trim();
                    autoLogin(b.checkLogin.isChecked());
                    loginViewModel.insertMyUserDB();
//                        editor = sp.edit();
//                        UserInfo.DataBean userInfo = loginViewModel.getUserlist();
//                            userInfo.getLastUpdateTime();
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    finish();
                    break;
                case FAIL:
                    ToastUtils.showShort("登录失败,用户名或密码或用户ID错误");
                    break;
            }
        });
        //点了自动登录必定会记住密码
        b.checkLogin.setOnCheckedChangeListener((buttonView, isChecked) -> {
            b.checkPass.setChecked(isChecked);
            b.checkPass.setEnabled(!isChecked);
        });
        //密码是否可见
        b.checkOpen.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                //密码可见
                b.editPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            } else {
                //密码不可见
                b.editPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }
        });
    }

    private void autoLogin(boolean bool) {
        editor = sp.edit();
        editor.putBoolean("checkLogin", bool);
        editor.apply();
    }

    private void rememberPassword(boolean bool) {
        editor = sp.edit();
        if (bool) {
            editor.putBoolean("checkPass", true);
            editor.putString("user", b.editUsername.getText().toString().trim());
            editor.putString("pass", b.editPassword.getText().toString().trim());
            editor.putString("userid", b.editUserId.getText().toString().trim());
        } else {
            editor.clear();
        }
        editor.apply();
    }
}
